package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.TblQLDTTXADMNhomService;
import com.lb.jwitter.service.TblQLDTTXADMPhongService;
import com.lb.jwitter.service.TblQLDTTXADMPhongThiService;
import com.lb.jwitter.service.TblQLDTTXADMTietHocService;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGK;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGKService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblQLDTTXALichKiemTraGiuaKyUpdateAction {
	private Long id; 
    public void setId(Long id) {
       this.id = id;
    }
    public Long getId() {
		return id;
	}
	
	private String ghiChu;
	public String getGhiChu(){
		return ghiChu;
	}
	public void setGhiChu(String ghiChu){
		this.ghiChu = ghiChu;
	}
	
	private String ngayThi;
	public String getNgayThi(){
		return ngayThi;
	}
	public void setNgayThi(String ngayThi){
		this.ngayThi = ngayThi;
	}
	
	private int thoiGian;
	public int getThoiGian(){
		return thoiGian;
	}
	public void setThoiGian(int thoiGian){
		this.thoiGian = thoiGian;
	}
	
	private int phongId;
	public int getPhongId(){
		return phongId;
	}
	public void setPhongId(int phongId){
		this.phongId = phongId;
	}
	
	private int lopId;
	public int getLopId(){
		return lopId;
	}
	public void setLopId(int lopId){
		this.lopId = lopId;
	}
	
	private int nhomId;
	
	public int getNhomId(){
		return nhomId;
	}
	public void setNhomId(int nhomId){
		this.nhomId = nhomId;
	}
	
	private int tietId;
	public int getTietId(){
		return tietId;
	}
	public void setTietId(int tietId){
		this.tietId = tietId;
	}
	
	private TblQLDTTXALichKiemTraGK tblQLDTTXALichKiemTraGK;
	public TblQLDTTXALichKiemTraGK getTblQLDTTXALichKiemTraGK(){
		return tblQLDTTXALichKiemTraGK;
	}

	private TblQLDTTXALichKiemTraGKService tblQLDTTXALichKiemTraGKService;
	
	private TblQLDTTXADMPhongThiService tblQLDTTXADMPhongThiService;
	
	private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService; 
	
	private TblQLDTTXADMNhomService tblQLDTTXADMNhomService;
	
	private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService;
	
	private TblTaiKhoan taiKhoanDangNhap; 
    public TblTaiKhoan getTaiKhoanDangNhap() { 
        return taiKhoanDangNhap; 
    } 
    public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
        this.taiKhoanDangNhap = taiKhoanDangNhap; 
    } 
    private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
    ApplicationContextProvider contextProvider; 
public String execute() throws Exception 
{ 
	try{ 	
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LichKiemTraGK", "sua")){ 
	        return "noprivilege"; 
	    }
	    tblQLDTTXALichKiemTraGKService = (TblQLDTTXALichKiemTraGKService) context.getBean("tbl_QLDTTXA_LichKiemTraGKClient");
	    
	    tblQLDTTXADMPhongThiService = (TblQLDTTXADMPhongThiService) context.getBean("tbl_QLDTTXA_DM_PhongThiClient"); 

        tblQLDTTXADMTietHocService = (TblQLDTTXADMTietHocService) context.getBean("tbl_QLDTTXA_DM_TietHocClient"); 

        tblQLDTTXADMNhomService = (TblQLDTTXADMNhomService) context.getBean("tbl_QLDTTXA_DM_NhomClient");
        
        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient");
        
        tblQLDTTXALichKiemTraGK = tblQLDTTXALichKiemTraGKService.gettblQLDTTXALichKiemTraGK(id);
        
        if (tblQLDTTXALichKiemTraGK == null){
        	return Action.ERROR;
        }
        
        tblQLDTTXALichKiemTraGK.setGhiChu(ghiChu);
        tblQLDTTXALichKiemTraGK.setThoiGian(thoiGian);
        if (ngayThi != null && !ngayThi.isEmpty()){
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
            Date dd = df.parse(ngayThi); 
            GregorianCalendar c = new GregorianCalendar(); 
            c.setTime(dd);
            XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            tblQLDTTXALichKiemTraGK.setNgayThi(d);
        }
        
        if (nhomId != 0){
        	tblQLDTTXALichKiemTraGK.setNhomId(tblQLDTTXADMNhomService.gettblQLDTTXADMNhom(new Long(nhomId)));
        }
        if (lopId != 0){
        	tblQLDTTXALichKiemTraGK.setLopId(tblQLDTTXALopMonHocService.gettblQLDTTXALopMonHoc(new Long(lopId)));
        }
        if (phongId != 0){
        	tblQLDTTXALichKiemTraGK.setPhongId(tblQLDTTXADMPhongThiService.gettblQLDTTXADMPhongThi(new Long(phongId)));
        }
        if (tietId != 0){
        	tblQLDTTXALichKiemTraGK.setTietThiId(tblQLDTTXADMTietHocService.gettblQLDTTXADMTietHoc(new Long(tietId)));
        }
        
        tblQLDTTXALichKiemTraGKService.updatetblQLDTTXALichKiemTraGK(tblQLDTTXALichKiemTraGK);
        
	    
        return Action.SUCCESS; 
}
	catch(Exception e){ 
	      System.out.println("Loi Insert tbl_QLDTTXA_LichKiemTraGK Action"); 
	     e.printStackTrace(); 
	    return Action.ERROR; 
	 } 
}
}
